import { getItem,setItem } from "@/utils/storage"
import {LANG,TAGS_VIEW} from '@/constant/index'
export default {
  namespaced: true,
  state: () => ({
    sidebarOpened: true,
    language:getItem(LANG) || 'zh',
    tagsViewList: getItem(TAGS_VIEW) || []
  }),
  mutations:{
    triggerSidebarOpened(state){
      state.sidebarOpened = !state.sidebarOpened
    },
    /**
     * 设置国际化
     */
     setLanguage(state, lang){
      setItem(LANG, lang)
      state.language = lang
    },
    /**
     * 添加 tags
     */
    addTagsViewList(state, tag){
      // 判断是否有重复数据
      const findTag = state.tagsViewList.find(item => tag.path == item.path)
      // 如果不是重复
      if(!findTag){
        state.tagsViewList.push(tag)
        setItem(TAGS_VIEW, state.tagsViewList)
      }
    },
    /**
    * 为指定的 tag 修改 title
    */
    changeTagsView(state,{index,tags}){
      state.tagsViewList[index] = tags
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    /**
     * 删除 tag
     * @param {type: 'other'||'right'||'current', index: index} payload
     * 'current':删除当前的
     * 'other'删除其他
     * 'right'删除右侧
     */
     removeTagsView(state, payload){
      if(payload.type == 'current'){
        state.tagsViewList.splice(payload.index,1)
        
      }else if(payload.type == 'other'){
        // state.tagsViewList = state.tagsViewList.filter(item=>{
        //   return item.index == payload.index
        // })
        state.tagsViewList.splice(payload.index + 1,state.tagsViewList.length - payload.index + 1)
        state.tagsViewList.splice(0,payload.index)
        
      }else if(payload.type == 'right'){
        state.tagsViewList.splice(payload.index + 1,state.tagsViewList.length - payload.index + 1)
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    },
  }
}